package com.icarsaas.saas.tenant.cars.customer.web;

import com.icarsaas.saas.common.page.SoulPage;
import com.icarsaas.saas.common.utils.ResultVoUtil;
import com.icarsaas.saas.common.vo.ResultVo;
import com.icarsaas.saas.component.actionLog.action.SaveAction;
import com.icarsaas.saas.component.actionLog.action.StatusAction;
import com.icarsaas.saas.component.actionLog.annotation.ActionLog;
import com.icarsaas.saas.component.actionLog.annotation.EntityParam;
import com.icarsaas.saas.tenant.cars.customer.model.Store;
import com.icarsaas.saas.tenant.cars.customer.model.XmSelectModel;
import com.icarsaas.saas.tenant.cars.customer.service.StoreService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @title: StoreController
 * @description: 店铺管理
 * @author xrong120@gmail.com
 * @date 2021年1月23日
 */
@Controller @Api(tags="门店模块") @RequestMapping(value = "/cars/store")
public class StoreController {
	@Autowired private StoreService storeService;
	
	@GetMapping("/storeList")
	public String storeList() {
		return "/store/storeList";
	}

	@PostMapping("listSoulTable") @ResponseBody
	public Object listSoulTable(SoulPage<Map<String, Object>> page, @RequestBody Map<String, Object> params) {
		page.setParams(params, page);
		page = storeService.listSoulData(page, params);
		return page;
	}

	@ModelAttribute
	public Store get(@RequestParam(required = false) String id) {
		Store entity = null;
		if (id != null) entity = storeService.get(id);
		else entity = new Store();
		return entity;
	}
	/**
	 * 跳转到编辑页面
	 */
	@GetMapping({"storeForm", "storeForm/{id}"})
	public String storeForm(@RequestParam(value = "id", required = false) Store entity, Model model) {
		if (entity != null) model.addAttribute("entity", entity);
		return "/store/storeForm";
	}
	/**
	 * 保存添加/修改的数据
	 */
	@PostMapping({"/saveEntity"}) @ResponseBody
	@ActionLog(name = "店铺管理", message = "店铺信息：${name}", action = SaveAction.class)
	public ResultVo<?> saveEntity(@EntityParam Store entity) {
		storeService.save(entity);
		return ResultVoUtil.SAVE_SUCCESS;
	}

	/**
	 * 删除记录
	 * @param ids 删除的数据id
	 * @param param 状态标识，必须带
	 * @return
	 */
	@PostMapping("/deleteEntity") @ResponseBody
	@ActionLog(name = "店铺管理", message = "删除店铺信息：${name}", action = StatusAction.class)
	public ResultVo<?> deleteEntity(@RequestParam(value = "ids", required = false) List<String> ids
			, @RequestParam(value = "param", required = false) String param) {
		// 更新状态
		if (ids != null && ids.size() > 0) {
			ids.forEach(id -> {
				storeService.delete(id);
			});
			return ResultVoUtil.success("操作成功");
		} else {
			return ResultVoUtil.error("操作失败，请重试!");
		}
	}

	/**
	 * 跳转到详细页面
	 */
	@GetMapping("/detail/{id}")
	public String toDetail(@PathVariable("id") Store entity, Model model){
		model.addAttribute("entity", entity);
		return "/store/storeDetail";
	}

	/**
	 * 店铺列表，返回json方式提供给xm-select使用
	 */
	@GetMapping({"/listStore"}) @ResponseBody
	public ResultVo<?> listStore() {
		List<Store> list = storeService.findAll();
		List<XmSelectModel> models = new ArrayList<XmSelectModel>();
		XmSelectModel model = null;
		if (list != null && list.size() > 0) {
			for (Store store : list) {
				model = new XmSelectModel();
				model.setValue(store.getId());
				model.setName(store.getName());
				models.add(model);
			}
		}
		return ResultVoUtil.success(models);
	}

}
